using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Web;

namespace CSSg2Help
{
    public class Http
    {
        private string host = "";
        private string account = "";
        private string password = "";
        private string serverid = "";

        public Http(string host,string account,string password,string serverid)
        {
            this.host = host;
            this.account = account;
            this.password = password;
            this.serverid = serverid;
        }

        public string GetUrl()
        {
            string request = "";
            string response = "";

            request = String.Format("GET / HTTP/1.1\r\nHost: {0}\r\n\r\n", this.host);
            response = GetHttpResponse(this.host, request);

            Match m = Regex.Match(response, "(?<=form_submit_key_time\"[^\"]+\")[^\"]+");
            string form_submit_key_time = m.ToString();

            Console.WriteLine(form_submit_key_time);

            m = Regex.Match(response, "(?<=form_submit_key_v1\"[^\"]+\")[^\"]+");
            string form_submit_key_v1 = m.ToString();

            Console.WriteLine(form_submit_key_v1);

            m = Regex.Match(response, "(?<=form_submit_key_v2\"[^\"]+\")[^\"]+");
            string form_submit_key_v2 = m.ToString();

            Console.WriteLine(form_submit_key_v2);

            m = Regex.Match(response, "(?<=fik1\"[^\"]+\")[^\"]+");
            string fik1 = m.ToString();

            Console.WriteLine(fik1);

            m = Regex.Match(response, "(?<=fik2\"[^\"]+\")[^\"]+");
            string fik2 = m.ToString();

            Console.WriteLine(fik2);

            int length = 324 + account.Length + password.Length + 19;
            request = String.Format("POST /user/login.phtml HTTP/1.1\r\nHost: {0}\r\nConnection: keep-alive\r\nContent-Length: {1}\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nOrigin: http://{2}\r\nUser-Agent: Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0\r\nContent-Type: application/x-www-form-urlencoded\r\nReferer: http://{3}/game/rxsg2.phtml\r\nAccept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\nAccept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\nCookie:  Hm_lvt_8c13a699eb6a7579b36b376356087e10=1362820884,1362821249; Hm_lpvt_8c13a699eb6a7579b36b376356087e10={4}\r\n\r\nact=submit&form_submit_key_time={5}&form_submit_key_v1={6}&form_submit_key_v2={7}&fik1={8}&fik2={9}&url=%%2Fgame%%2Frxsg2.phtml&{10}={11}&{12}={13}&myaction=1&button=", host, length.ToString(), host, host, form_submit_key_time, form_submit_key_time, form_submit_key_v1, form_submit_key_v2, fik1, fik2, fik1, account, fik2, password);
            response = GetHttpResponse(this.host, request);

            Console.WriteLine(response);

            m = Regex.Match(response, "(?<=Set-Cookie: )online_key=[a-f0-9]{32};");
            string cookie1 = m.ToString();

            Console.WriteLine(cookie1);

            m = Regex.Match(response, "(?<=Set-Cookie: )online_[a-f0-9]{16}=[a-f0-9]{32}");
            string cookie2 = m.ToString();

            Console.WriteLine(cookie2);

            request = String.Format("GET /server/login/{0}.phtml HTTP/1.1\r\nHost: {1}\r\nUser-Agent: Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\nReferer: http://{2}/game/rxsg2.phtml\r\nCookie: Hm_lvt_8c13a699eb6a7579b36b376356087e10=1361625967,1361687588,1361720342,1361796680; Hm_lpvt_8c13a699eb6a7579b36b376356087e10={3}; {4} {5}\r\nConnection: keep-alive\r\n\r\n", this.serverid, this.host, this.host, form_submit_key_time, cookie1, cookie2);
            response = GetHttpResponse(this.host, request);

            Console.WriteLine(response);

            m = Regex.Match(response, "(?<=<body><a href=\".*url=)[^\"]+");
            Console.WriteLine(HttpUtility.UrlDecode(m.ToString(), System.Text.Encoding.UTF8));

            return HttpUtility.UrlDecode(m.ToString(), System.Text.Encoding.UTF8);
        }

        private string GetHttpResponse(string host,string request)
        {
            byte[] recv = new byte[1024 * 1024];
            IPHostEntry ipHost = Dns.GetHostEntry(host);
            IPAddress ipAddress = ipHost.AddressList[0];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 80);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(ipEndPoint);
            s.Send(Encoding.UTF8.GetBytes(request));
            int length = s.Receive(recv);
            string response = Encoding.UTF8.GetString(recv,0,length);
            s.Shutdown(SocketShutdown.Both);
            s.Close();
            return response;
        }
    }
}
